//
//  CaoZuoJiLuListVC.m
//  EnterpriseEdition
//
//  Created by ypj on 2021/10/9.
//

#import "CaoZuoJiLuListVC.h"
#import "RenYuanGuanLiCell.h"
#import "CaoZuoDetailVC.h"
#import "RenYanGuanLiModel.h"
@interface CaoZuoJiLuListVC ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource, DZNEmptyDataSetDelegate>
@property (nonatomic,strong)  UITableView       *  tableView;
@property (nonatomic, strong) NSMutableArray    *  dataAry;
@property (nonatomic, assign) NSInteger            nowPage;
@property (nonatomic, assign) NSInteger            totalPage;
@end

@implementation CaoZuoJiLuListVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self layoutCustomNav:self.view title:@"操作记录"];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = White_Color;
    [self dataAry];
    [self getCaoZuoRenYuanList];
    self.userInfo=[YLUserMgr fetchUser];
    [self createTableView];
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self getCaoZuoRenYuanList];
    }];
    header.lastUpdatedTimeLabel.hidden = YES;
    self.tableView.mj_header = header;
}

-(void)createTableView{
    self.tableView=[[UITableView  alloc] initWithFrame:CGRectMake(0, LL_StatusBarAndNavigationBarHeight, ScreenWidth, B_HEIGHT-LL_StatusBarAndNavigationBarHeight-LL_TabbarSafeBottomMargin)];
    self.tableView.delegate=self;
    self.tableView.dataSource=self;
    self.tableView.backgroundColor = White_Color;
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableView registerNib:[UINib nibWithNibName:[RenYuanGuanLiCell reuseIdentifier] bundle:nil] forCellReuseIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    [self.view addSubview:self.tableView];
    
}

#pragma mark - ------------------ DZNEmptyData ------------------
-(UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"No_order_icon"];
}
- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *title = @"暂无数据";
    NSDictionary *attributes = @{
                                 NSFontAttributeName:[UIFont systemFontOfSize:15.0f],
                                 NSForegroundColorAttributeName:gray666_color
                                 };
    return [[NSAttributedString alloc] initWithString:title attributes:attributes];
}

- (void)emptyDataSet:(UIScrollView *)scrollView didTapView:(UIView *)view {
    if (!self.tableView.mj_header.isRefreshing && !self.tableView.mj_footer.isRefreshing) {
        [self.tableView.mj_footer resetNoMoreData];
        self.nowPage = 1;
        [self getCaoZuoRenYuanList];
    }
}

- (CGFloat)verticalOffsetForEmptyDataSet:(UIScrollView *)scrollView {
    return -50;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return  self.dataAry.count;
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
     return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    RenYuanGuanLiCell  *  cell=[tableView dequeueReusableCellWithIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    if (!cell) {
            cell=[[RenYuanGuanLiCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:[RenYuanGuanLiCell reuseIdentifier]];
    }
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    RenYanGuanLiModel *  model=self.dataAry[indexPath.row];
    cell.bieMingLb.text=model.nickName;
    cell.jueSeLb.text=model.roleName;
    cell.nameLb.text=model.userName;
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
        return [RenYuanGuanLiCell cellHeight];
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CaoZuoDetailVC  *  VC =[[CaoZuoDetailVC alloc] init];
    VC.model=self.dataAry[indexPath.row];
    [self pushToNextController:VC];
}

#pragma mark ----------------<#note#>----------------
- (NSMutableArray *)dataAry {
    if (!_dataAry) {
        self.dataAry = [@[] mutableCopy];
    }
    return _dataAry;
}

-(void)getCaoZuoRenYuanList{
    NSMutableDictionary *params=[NSMutableDictionary dictionary];
    if ([kUserDefaultForKey(EduitString_key) isEqualToString:EduitString]){
        //超级管理员查看全部
        [params setObject:@"0" forKey:@"type"];
    } else {
        //非超级管理员查看自己
        [params setObject:@"1" forKey:@"type"];
    }
    
    [MSNetworkManager postRevertUrl:[NSString stringWithFormat:@"%@per/list", CaoZuo_Url] params:params success:^(id  _Nonnull paginationObj, id  _Nonnull resonseObj, BOOL isSuccess, NSString * _Nonnull message) {
        
        if (isSuccess) {
            [self.tableView.mj_header endRefreshing];
             NSArray  *cacheArray = resonseObj;
             self.dataAry = [RenYanGuanLiModel  mj_objectArrayWithKeyValuesArray:cacheArray];
            [self.tableView reloadData];
        }
        
        
    } failure:^(NSUInteger code, NSString * _Nonnull message) {
    } withShowHud:YES];
    
 
}

@end
